#include "stdafx.h"
#include "NormalTaskTrigger.h"

#ifdef WIN32
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif //_DEBUG
#endif //WIND32

namespace serika
{
	namespace ut
	{
		NormalTaskTrigger::NormalTaskTrigger(size_t start, size_t end, size_t loopInterval)
			: mStart(start)
			, mEnd(end)
			, mLooInterval(loopInterval)
			, mInit(false)
			, mInfinity(false)
		{
		}

		NormalTaskTrigger::~NormalTaskTrigger()
		{
		}

		bool NormalTaskTrigger::CanExecute(const Clock& clock)
		{
			if (!mInit)
			{
				this->Init(clock);
				mInit = true;
			}

			size_t curr = this->GetCurr(clock);

			if (curr < mStart)
			{
				return false;
			}

			this->CalcNextStart(clock);
			return true;
		}

		bool NormalTaskTrigger::CanTerminate(const Clock& clock) const
		{
			return !mInfinity && mStart >= mEnd;
		}

		void NormalTaskTrigger::Init(const Clock& clock)
		{
			mInfinity = (0 == mEnd);

			mStart += this->GetCurr(clock);
			mEnd += this->GetCurr(clock);
		}

		void NormalTaskTrigger::CalcNextStart(const Clock& clock)
		{
			mStart = mLooInterval + this->GetCurr(clock);
		}
	}
}
